<template>
  <el-dialog
    v-model="depositEditDialogVisible"
    title="修改抵押信息"
    width="40%"
    center
    @close="closeDialog(false)"
  >
    <el-form
      ref="validateForm"
      :model="dealerForm"
      :rules="rules"
      label-width="120px"
      :status-icon="true"
    >

       <el-form-item label="押金金额" prop="deposit">
        <el-input v-model="dealerForm.deposit" style="width:150px;" ></el-input>（元）
      </el-form-item>

      <el-form-item label="抵押商品" prop="productId">
       
        <el-select
            v-model="dealerForm.productId" 
            filterable
            placeholder="选择商品"
          >

          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>  

      <el-form-item label="抵押数量" prop="productCount">
        <el-input-number v-model="dealerForm.productCount" size="mini" :min="1" :max="10"  />
         
      </el-form-item>

   <el-form-item label="押金时间" prop="depositDate">
        <el-date-picker v-model="dealerForm.depositDate" value-format="YYYY-MM-DD" format="YYYY-MM-DD" type="date" placeholder="选择时间">
      </el-date-picker>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" @click="submitForm">修改</el-button>
        <el-button
          @click="
            () => {
              closeDialog(false);
            }
          "
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";
import { putRequest,getRequest,formatNowDate } from "../../utils/api";

function dealerAdd(state, validateForm, emit) {
  const submitForm = () => {
    validateForm.value.validate((valid) => {
      if (valid) {
        if(state.dealerForm.dealerId){
          putRequest("/sell/dealer/deposit", state.dealerForm).then((res) => {
          if (res && res.code == 200) {
            validateForm.value.resetFields();
            emit("onCloseEditDepositVisible", false, res.data);
          } else {
            ElMessage({
              showClose: true,
              message: res.message,
              type: "error",
            });
          }
        });
        }else{
           ElMessage({
              showClose: true,
              message: '未选中经销商',
              type: "error",
            });
        }
        
      }
    });
  };
  return { submitForm };
}

 

export default {
  components: {},
  props: {
    depositId:String,
    depositEditDialogVisible: Boolean,
  },

  setup(props, { emit }) {
    const validateForm = ref();
    const state = reactive({
      depositEditDialogVisible: props.depositEditDialogVisible,
      dealerForm: {
        deposit: 0.0,
        productCount: 0,
        productId:'',
        depositDate: formatNowDate(),
        dealerId:'',
        id:props.depositId
      },
      productList:[],
      loading:false
    });

    const rules = {
      deposit: [{ required: true, message: "请输入抵押金额", trigger: "blur" }],
       productId: [{ required: true, message: "请选择商品", trigger: "blur" }],
      productCount: [{ required: true, message: "请输入商品数量", trigger: "blur" }],
      depositDate: [{ required: true, message: "请选择时间", trigger: "blur" }],
      
     
    };

    const closeDialog = (visible, data) => {
      validateForm.value.resetFields();
      emit("onCloseEditDepositVisible", visible, data);
    };
    const loadProductData=()=>{
       getRequest('/product/basic/listAll',{keywords:''}).then(response => {
              if (response) {
                state.productList = response.data;
              }
            })
        
      
    }
    const loadDepositData = ()=>{
         getRequest("/sell/dealer/deposit/one", { id: state.dealerForm.id }).then((res) => {
            if (res && res.code == 200) {
            state.dealerForm = res.data;
            } else {
            ElNotification({
                type: "error",
                title: "失败",
                message: "查询失败",
                duration: 2000,
            });
            }

     
        });
    }
    onMounted(()=>{
      loadProductData() ;
      loadDepositData()
    })
    return {
      ...toRefs(state),
      closeDialog,
      rules,
      ...dealerAdd(state, validateForm, emit), 
      validateForm
   
    };
  },
};
</script>

<style>

.el-input-number__increase{
  margin-top:1px;
}

.el-input-number__decrease{
  margin-top:1px;
}


.el-form-item__error {
  padding-top: 0px;
}
.el-input__inner {
  height: 32px;
  line-height: 32px;
}
.el-form-item {
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 18px;
  color: #8c939d;
  width: 88px;
  height: 88px;
  line-height: 88px;
  text-align: center;
}
.avatar {
  width: 88px;
  height: 88px;
  display: block;
}
</style>